<template>
  <div>
    <el-form :model="formData" label-width="120px" :rules="FormRules" ref="addForm">
      <el-form-item label="供应商编码" prop="supplierCode">
        <el-input v-model="formData.supplierCode" placeholder="请输入供应商编码"></el-input>
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input v-model="formData.supplierName" placeholder="请输入供应商名称"></el-input>
      </el-form-item>
      <el-form-item label="合同价" prop="contractPrice">
        <el-input v-model="formData.contractPrice" placeholder="请输入合同价"></el-input>
      </el-form-item>
      <el-form-item label="税率" prop="taxRate">
        <el-input v-model="formData.taxRate" placeholder="请输入税率"></el-input>
      </el-form-item>
      <el-form-item label="含税价" prop="taxInclusivePrice">
        <el-input v-model="formData.taxInclusivePrice" placeholder="请输入含税价"></el-input>
      </el-form-item>
      <el-form-item label="生效日期" prop="expirationDate">
        <el-date-picker
          v-model="formData.expirationDate"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="失效日期" prop="effectiveDate">
        <el-date-picker
          v-model="formData.effectiveDate"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" placeholder="请输入备注"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handSave()">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { supplierEdit, supplierAdd } from "../../../api/baseSetting";
export default {
  data() {
    var checkSort = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入排序"));
      }
      if (!Number.isInteger(value * 1)) {
        callback(new Error("请输入数字值"));
      } else {
        callback();
      }
    };
    return {
      formData: {
        supplierCode: "",
        supplierName: "",
        contractPrice: "",
        taxRate: "",
        taxInclusivePrice: "",
        expirationDate: "",
        effectiveDate: "",
        remark: ""
      },
      FormRules: {
        supplierCode: [
          { required: true, message: "请输入供应商编码", trigger: "blur" }
        ],
        supplierName: [
          { required: true, message: "请输入供应商名称", trigger: "blur" }
        ],
        contractPrice: [
          { required: true, message: "请输入合同价", trigger: "blur" }
        ],
        taxRate: [{ required: true, message: "请输入税率", trigger: "blur" }],
        taxInclusivePrice: [
          { required: true, message: "请输入含税价", trigger: "blur" }
        ],
        expirationDate: [
          { required: true, message: "请选择生效日期", trigger: "blur" }
        ],
        effectiveDate: [
          { required: true, message: "请选择失效日期", trigger: "blur" }
        ],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }]
      }
    };
  },
  props: {
    sendFormData: {
      num: {
        type: Object
      }
    }
  },
  methods: {
    //点击保存
    handSave() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          if (this.formData.id) {
            //编辑
            supplierEdit(this.formData).then(res => {
              this.$message({
                message: "修改成功",
                type: "success"
              });
              this.hideTotas();
              this.$emit("hideTotas");
            });
          } else {
            //新增
            supplierAdd(this.formData).then(res => {
              this.$message({
                message: "新增成功",
                type: "success"
              });
              this.hideTotas();
              this.$emit("hideTotas");

            });
          }
        }
      });
    },
    //重置表单
    hideTotas(){
      this.$refs.addForm.resetFields();
    }
  },
  mounted() {
    console.log(this.sendFormData);
    this.formData = this.sendFormData;
  }
};
</script>

<style lang="less" scoped>
</style>
